import { pageSynchronyLog, updateSynchrony } from '@/services/api/api-datahub-synchrony';
import { SnippetsOutlined } from '@ant-design/icons';
import { ActionType, DrawerForm, ProColumns, ProTable } from '@ant-design/pro-components';
import { useAccess } from 'umi';
import { Button,  message } from 'antd';
import moment from 'moment';
import React, { useRef } from 'react';

interface LogSourceProps {
  onRefurbish: () => void;
  detailData: DatahubSynchronyData.SynchronyPage;
}

const LogSource: React.FC<LogSourceProps> = ({ onRefurbish, detailData }) => {
  const access = useAccess();

  const actionRef = useRef<ActionType>();

  const columns: ProColumns<DatahubSynchronyData.SynchronyLogPage>[] = [
    {
      title: '任务描述',
      dataIndex: 'jobDesc',
      ellipsis: true,
      search: false,
      align: 'center',
    },
    {
      title: '调度时间',
      dataIndex: 'handleTime',
      key: 'handleTime',
      align: 'center',
      valueType: 'dateTime',
      search: false,
      width: 180
    },
    {
      title: '调度时间',
      dataIndex: 'triggerTimes',
      key: 'triggerTimes',
      align: 'center',
      valueType: 'dateRange',
      hideInTable: true,
      width: 180,
      fieldProps: {
        allowClear: true,
        format: 'YYYY-MM-DD',
        placeholder: ['开始日期', '结束日期'],
        ranges: {
          今日: [moment(), moment()],
          本月: [moment().startOf('month'), moment().endOf('month')],
          上月: [
            moment().startOf('month').subtract(1, 'months'),
            moment().endOf('month').subtract(1, 'months'),
          ],
          最近7天: [moment().subtract(6, 'days'), moment()],
          最近60天: [moment().subtract(59, 'days'), moment()],
          最近90天: [moment().subtract(89, 'days'), moment()],
        }
      }
    },
    {
      title: '调度结果',
      dataIndex: 'triggerCode',
      key: 'triggerCode',
      align: 'center',
      search: false,
      valueType: 'select',
      valueEnum: {
        200: { text: '成功', status: 'Success' },
        0: { text: '失败', status: 'Error' },
      },
    },
    {
      title: '执行结果',
      dataIndex: 'handleCode',
      ellipsis: true,
      search: false,
      align: 'center',
      valueType: 'select',
      valueEnum: {
        200: { text: '成功', status: 'Success' },
        0: { text: '失败', status: 'Error' },
      },
    },
    {
      title: '执行时间',
      dataIndex: 'triggerTime',
      key: 'triggerTime',
      align: 'center',
      search: false,
      width: 180
    },
  ];

  return (
    <DrawerForm
      title="调度日志"
      resize={{
        onResize() {
          console.log('resize!');
        },
        maxWidth: window.innerWidth * 0.8,
        minWidth: 700,
      }}
      trigger={
        detailData.id ? <a>调度日志</a> : <Button
          type="primary"
        >
          <SnippetsOutlined />
          调度日志
        </Button>
      }
      initialValues={{
        name: detailData.jobDesc,
        factoryConfig: detailData.factoryConfig,
        cron: detailData.scheduleConf,
      }}
      autoFocusFirstInput
      drawerProps={{
        destroyOnClose: true,
        width: 700,
      }}
      submitter={false}
      onFinish={async (values) => {
        // const response = await updateSynchrony({
        //   ...values,
        //   id: detailData.id,
        //   executorParam: detailData.executorParam,
        // });
        // if (response.code === 0) {
        //   message.success('提交成功');
        //   onRefurbish();
        //   return true;
        // } else {
        //   return false;
        // }
      }}
    >
      <ProTable<DatahubSynchronyData.SynchronyLogPage>
        search={{
          // labelWidth: 'auto',
          // optionRender: false,
          // collapsed: false,
        }}
        columns={columns}
        actionRef={actionRef}
        request={async (params = {}) => {
          const response = await pageSynchronyLog({ ...params, id: detailData.id });

          return Promise.resolve({
            data: response.data.data,
            total: response.data.total,
            success: response.code === 0,
          });
        }}
        rowKey="id"
        dateFormatter="string"
        bordered
        ghost
        pagination={{
          showQuickJumper: true,
          showSizeChanger: true,
          defaultPageSize: 20,
        }}
      />
    </DrawerForm>
  );
};
export default LogSource;
